package com.csy.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csy.common.domain.LoginUser;
import com.csy.common.domain.R;
import com.csy.system.entity.User;
import com.csy.system.service.UserService;
import com.csy.system.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2020-12-07
 */
@Api("用户管理")
@AllArgsConstructor
@RestController
@RequestMapping("/system/user")
public class UserController {
    private final UserService userService;

    @ApiOperation(value = "用户列表")
    @GetMapping("findAll")
    public R findAllTeacher() {
        List<User> list = userService.list(null);
        return R.ok(list);
    }


    @ApiOperation(value = "用户列表")
    @GetMapping("page/{page}/{size}")
    public R page(@PathVariable Integer page,@PathVariable Integer size) {
        Page<User> userPage = userService.page(new Page(page, size),null);
        return R.ok(userPage);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping
    public R add(@RequestBody User user) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //user.setCreatorId(user1.getId().toString());
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        userService.save(user);
        return R.ok("添加成功");
    }

}

